var grassEffectVertexShaderStr = "\
attribute vec3 inPosition;\
attribute vec2 inTexCoord0;\
attribute vec2 inOffset;\
uniform mat4 viewProjectionMat;\
uniform vec4 vertexParam0;\
uniform vec4 vertexParam1;\
uniform vec3 camRightVec;\
uniform vec3 camUpVec;\
uniform vec3 viewPosition;\
varying vec2 outTexCoord0;\
varying float distanceFromCam;\
void main(void)\
{\
  vec3 localPos = inPosition+ inOffset.x*camRightVec+inOffset.y*camUpVec;\
  if(inTexCoord0.y>0.2)\
    localPos+=(vertexParam0.xyz+sin(vertexParam0.w+inPosition.x+inPosition.z)*vertexParam1.xyz);\
  gl_Position = viewProjectionMat*vec4(localPos,1.0);\
  outTexCoord0 = inTexCoord0;\
  distanceFromCam = length(viewPosition-inPosition);\
}\
";
var grassEffectFragmentShaderStr = "\
uniform sampler2D grassPackTexture;\
uniform float alphaRef;\
varying vec2 outTexCoord0;\
varying float distanceFromCam;\
void main(void)\
{\
  vec4 color = texture2D(grassPackTexture,outTexCoord0);\
  float f = 1.0-(clamp(distanceFromCam,0.0,1000.0)/1000.0);\
  if(color.a < alphaRef*f)\
    discard;\
  gl_FragColor = vec4( color.rgb,color.a);\
}\
";
function gewGrassEffect(game)
{
  // General properties
  this.game = game;
  this.gl = game.gl;
  var gl = game.gl;
  this.name = "GrassEffect";
  this.scene3d = null;//Current scene3d that using this effect 
  this.materialList = {};
  this.currentPass = null;
   
  // Shader properties
  createGLSLShader(gl,this, grassEffectVertexShaderStr, grassEffectFragmentShaderStr);
}
gewGrassEffect.prototype.destructor = function()
{
  var list  = this.materialList;
  var listLen = list.length;  
  for (var i in list)
  {
    list[i].destructor();
  }
  this.scene3d.removeEffect(this);
},
gewGrassEffect.prototype.render = function(scene3d) 
{
  var gl = this.gl; 
  this.scene3d = scene3d;
  // push GL state
  // set GL state
  gl.useProgram(this.program);
  gl.enableVertexAttribArray(this.inPosition);
  gl.enableVertexAttribArray(this.inTexCoord0);
  gl.enableVertexAttribArray(this.inOffset);
  gl.enable(gl.DEPTH_TEST);
  //gl.enable(gl.SAMPLE_ALPHA_TO_COVERAGE);
  gl.enable(gl.BLEND);
  gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  this.currentPass = 0;
  var materialList = this.materialList;
  var listLen = materialList.length;
  var viewProj = scene3d.viewProjectMat;
  
  var active_cam = scene3d.camera_system.getActiveCamera();
  var camRightVec = active_cam.m_right;
  var camUpVec = active_cam.m_up;
  gl.uniform3f(this.camRightVec, camRightVec.x,camRightVec.y,camRightVec.z);
  gl.uniform3f(this.camUpVec, camUpVec.x,camUpVec.y,camUpVec.z);
  gl.uniformMatrix4fv(this.viewProjectionMat, false, viewProj.data);
  gl.uniform1i(this.grassPackTexture, 0);
  //var viewPos = scene3d.viewPosition;
  //gl.uniform3f(this.viewPosition, viewPos.x, viewPos.y, viewPos.z);
  for(var i in materialList)
  { 
    materialList[i].render();
  }
  //
  gl.currentPass = null;
  // pop GL state  
  gl.disable(gl.DEPTH_TEST);
  //gl.disable(gl.SAMPLE_ALPHA_TO_COVERAGE);
  gl.disable(gl.BLEND);
  gl.disableVertexAttribArray(this.inPosition);
  gl.disableVertexAttribArray(this.inTexCoord0);
  gl.disableVertexAttribArray(this.inOffset);
}
